package tests;

import common.Utils;
import org.openqa.selenium.Alert;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

import java.io.IOException;
import java.time.Duration;

/**
 * @Author xuejiao
 * Data 2025/6/1 16:10
 */
public class PageByNoLogin extends Utils {
    public static String activityurl = "http://localhost:8080/activities-list.html";
    public static String addactivityurl = "http://localhost:8080/create-activity.html";
    public static String prizelisturl = "http://localhost:8080/prizes-list.html";
    public static String addprizeurl = "http://localhost:8080/create-prizes.html";
    public static String userlisturl = "http://localhost:8080/user-list.html";
    public static String addnormaluserurl = "http://localhost:8080/register.html";
    public PageByNoLogin() {
        super();
    }

    public void ActivityPageByNoLogin() throws IOException {
        WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(1));
        driver.get(activityurl);
        //列表未登录处理
        // 3. 立即处理 alert
//        try {
//            Alert alert = wait.until(ExpectedConditions.alertIsPresent());
//            System.out.println("弹窗内容：" + alert.getText());
//
//            alert.accept();
//        } catch (Exception e) {
//            System.out.println("未检测到 alert 弹窗！");
//        }

        handleAllAlertsAndWaitForLogin(driver);
//
//        // 4. 等待跳转到登录页面（根据页面 title 判断）
//        wait.until(ExpectedConditions.titleContains("登录"));
//        String title = driver.getTitle();
//        System.out.println("当前页面标题：" + title);
//        assert title.equals("管理员登录页面");

        // 5. 截图
        getScreenShot(PageByNoLogin.class.getName());


    }

    public void AddActivityByNoLogin() throws IOException {
        WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(1));
        driver.get(addactivityurl);
        //列表未登录处理
        // 3. 立即处理 alert
        handleAllAlertsAndWaitForLogin(driver);


        // 5. 截图
        getScreenShot(PageByNoLogin.class.getName());

    }


    public void PrizeListByNoLogin() throws IOException {
        WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(1));
        driver.get(prizelisturl);
        //列表未登录处理
        // 3. 立即处理 alert
        handleAllAlertsAndWaitForLogin(driver);



        // 5. 截图
        getScreenShot(PageByNoLogin.class.getName());

    }

    public void AddPrizeByNoLogin() throws IOException {
        WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(1));
        driver.get(addprizeurl);
        //列表未登录处理
        // 3. 立即处理 alert
        handleAllAlertsAndWaitForLogin(driver);



        // 5. 截图
        getScreenShot(PageByNoLogin.class.getName());

    }


    public void UserListByNoLogin() throws IOException {
        WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(1));
        driver.get(userlisturl);
        //列表未登录处理
        // 3. 立即处理 alert
        handleAllAlertsAndWaitForLogin(driver);




        // 5. 截图
        getScreenShot(PageByNoLogin.class.getName());

    }



    public void AddNormalUserByNoLogin() throws IOException {
        WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(1));
        driver.get(addnormaluserurl);
        //列表未登录处理
        // 3. 立即处理 alert
        handleAllAlertsAndWaitForLogin(driver);



        // 5. 截图
        getScreenShot(PageByNoLogin.class.getName());

    }

   /* public static String activityurl = "http://localhost:8080/activities-list.html";
    public static String addactivityurl = "http://localhost:8080/create-activity.html";
    public static String prizelisturl = "http://localhost:8080/prizes-list.html";
    public static String addprizeurl = "http://localhost:8080/create-prizes.html";
    public static String userlisturl = "http://localhost:8080/user-list.html";
    public static String addnormaluserurl = "http://localhost:8080/register.html";

    public PageByNoLogin() {
        super();
    }

    public void ActivityPageByNoLogin() throws IOException {
        WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10));

        driver.get(activityurl);


        wait.until(ExpectedConditions.titleContains("登录"));
        String title = driver.getTitle();
        System.out.println("当前页面标题：" + title);
        assert title.equals("管理员登录页面");
        getScreenShot(PageByNoLogin.class.getName());

        driver.quit();
    }

    public void AddActivityByNoLogin() throws IOException {
        WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10));
        handleAlertIfExists();
        driver.get(addactivityurl);
        handleAlertIfExists();

        wait.until(ExpectedConditions.titleContains("登录"));
        String title = driver.getTitle();
        System.out.println("当前页面标题：" + title);
        assert title.equals("管理员登录页面");
        getScreenShot(PageByNoLogin.class.getName());
        driver.quit();

    }

    public void PrizeListByNoLogin() throws IOException {
        WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10));
        handleAlertIfExists();
        driver.get(prizelisturl);
        handleAlertIfExists();


        wait.until(ExpectedConditions.titleContains("登录"));
        String title = driver.getTitle();
        System.out.println("当前页面标题：" + title);
        assert title.equals("管理员登录页面");
        getScreenShot(PageByNoLogin.class.getName());
        driver.quit();

    }

    public void AddPrizeByNoLogin() throws IOException {
        WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10));
        handleAlertIfExists();
        driver.get(addprizeurl);
        handleAlertIfExists();

        wait.until(ExpectedConditions.titleContains("登录"));
        String title = driver.getTitle();
        System.out.println("当前页面标题：" + title);
        assert title.equals("管理员登录页面");
        getScreenShot(PageByNoLogin.class.getName());
        driver.quit();

    }

    public void UserListByNoLogin() throws IOException {
        WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10));
        handleAlertIfExists();
        driver.get(userlisturl);
        handleAlertIfExists();
        wait.until(ExpectedConditions.titleContains("登录"));
        String title = driver.getTitle();
        System.out.println("当前页面标题：" + title);
        assert title.equals("管理员登录页面");
        getScreenShot(PageByNoLogin.class.getName());
        driver.quit();

    }

    public void AddNormalUserByNoLogin() throws IOException {
        WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10));
        handleAlertIfExists();
        driver.get(addnormaluserurl);
        handleAlertIfExists();

        wait.until(ExpectedConditions.titleContains("登录"));
        String title = driver.getTitle();
        System.out.println("当前页面标题：" + title);
        assert title.equals("管理员登录页面");
        getScreenShot(PageByNoLogin.class.getName());
        driver.quit();

    }*/
}
